Explicitly defining a cursor as read-only can improve performance by avoiding table locking. This allows other SQL requests to execute in parallel.
Therefore when a cursor will only be used to read data, without modifying anything, the FOR READ ONLY
clause or its synonyn, FOR
FETCH ONLY
, should be used.
Conversely when a cursor will modify data, that too should be specified using the FOR UPDATE
clause.
In short, it’s better to always explicitly define the purpose of the cursor with help of the FOR READ ONLY
, FOR FETCH
ONLY
or FOR UPDATE
clauses.
Noncompliant code example
EXEC SQL DECLARE CMAJ_0A CURSOR
FOR SELECT C_BQ
FROM S1ORDCOU
WHERE C_BQ = :TORD-C-BQ
END-EXEC
Compliant solution
EXEC SQL DECLARE CMAJ_0A CURSOR
FOR SELECT C_BQ
FROM S1ORDCOU
WHERE C_BQ = :TORD-C-BQ
FOR READ ONLY
END-EXEC